package com.wgchao.amc.controller.base;

import com.wgchao.amc.common.BindingError;
import com.wgchao.amc.common.MenuAutho;
import com.wgchao.amc.common.PageData;
import com.wgchao.amc.common.ResultCommon;
import com.wgchao.amc.common.validate.Save;
import com.wgchao.amc.common.validate.Update;
import com.wgchao.amc.mapstruct.dto.SysBaseFuncDTO;
import com.wgchao.amc.mapstruct.dto.SysBaseMenuDTO;
import com.wgchao.amc.mapstruct.dto.SysMenuProjectDTO;
import com.wgchao.amc.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @作者：wgchao
 * @时间：21:58 2019/10/29 0029
 * @用途：菜单管理
 **/
@RestController
@RequestMapping("/amc/sys/base/menu")
@Api(tags = "菜单管理")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @PostMapping("/save")
    @ApiOperation(value = "新增菜单")
    @MenuAutho(desc = "新增菜单")
    @ApiParam(name = "menuDTO", value = "菜单信息", required = true)
    public ResultCommon<SysBaseMenuDTO> save(@RequestBody @Validated(value = {Save.class}) SysBaseMenuDTO menuDTO,
                                             BindingResult bindingResult) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        return menuService.save(menuDTO);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询菜单信息")
    @MenuAutho(desc = "分页查询菜单信息")
    @ApiParam(name = "proMark", value = "项目标示", required = true)
    public ResultCommon<PageData<SysBaseMenuDTO>> page(String proMark, int page, int limit) throws Exception{
        return menuService.page(proMark, page, limit);
    }

    @GetMapping("/search/{menuId}")
    @ApiOperation(value = "查询菜单详细信息")
    @MenuAutho(desc = "查询菜单详细信息")
    @ApiParam(name = "menuId", value = "菜单ID", required = true)
    public ResultCommon<SysBaseMenuDTO> search(@PathVariable Long menuId) throws Exception{
        return menuService.search(menuId);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新菜单信息", notes = "菜单名称、菜单路径")
    @MenuAutho(desc = "更新菜单信息")
    @ApiParam(name = "menuDTO", value = "菜单信息", required = true)
    public ResultCommon<SysBaseMenuDTO> update(@RequestBody @Validated(value = {Update.class}) SysBaseMenuDTO menuDTO,
                                               BindingResult bindingResult) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        return menuService.update(menuDTO);
    }

    @DeleteMapping("/delete/{menuId}")
    @ApiOperation(value = "删除菜单信息")
    @MenuAutho(desc = "删除菜单信息")
    @ApiParam(name = "menuId", value = "菜单ID", required = true)
    public ResultCommon delete(@PathVariable Long menuId) throws Exception{
        return menuService.delete(menuId);
    }


    @GetMapping("/pro/page")
    @ApiOperation(value = "分页查询项目列表")
    @MenuAutho(desc = "分页查询项目列表")
    public ResultCommon<PageData<SysMenuProjectDTO>> page(int page, int limit) throws Exception{
        return menuService.page(page, limit);
    }

    /**
     * 新增项目信息
     * @param projectDTO
     * @param bindingResult
     * @return
     * @throws Exception
     */
    @PostMapping("/pro/save")
    @ApiOperation(value = "新增项目信息")
    @MenuAutho(desc = "新增项目信息")
    public ResultCommon<SysMenuProjectDTO> save(@RequestBody @Validated(value = {Save.class}) SysMenuProjectDTO projectDTO,
                                                BindingResult bindingResult) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        return  menuService.save(projectDTO);
    }

    /**
     * 删除项目信息
     * @return
     * @throws Exception
     */
    @DeleteMapping("/pro/delete/{proid}")
    @ApiOperation(value = "删除项目信息")
    @MenuAutho(desc = "删除项目信息")
    public ResultCommon<SysMenuProjectDTO> deletePro(@PathVariable Long proid) throws Exception{
        return  menuService.deletePro(proid);
    }

    /**
     * 新增项目信息
     * @param projectDTO
     * @param bindingResult
     * @return
     * @throws Exception
     */
    @PutMapping("/pro/update")
    @ApiOperation(value = "更新项目信息")
    @MenuAutho(desc = "更新项目信息")
    public ResultCommon<SysMenuProjectDTO> update(@RequestBody @Validated(value = {Update.class}) SysMenuProjectDTO projectDTO,
                                                BindingResult bindingResult) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        return  menuService.update(projectDTO);
    }

    @GetMapping("/pro/all")
    @ApiOperation(value = "查询全部项目")
    @MenuAutho(desc = "查询全部项目")
    public ResultCommon<List<SysMenuProjectDTO>> searchAll() throws Exception{
        return menuService.searchAll();
    }


    @GetMapping("/func/page")
    @ApiOperation(value = "分页查询功能列表")
    @MenuAutho(desc = "分页查询功能列表")
    public ResultCommon<PageData<SysBaseFuncDTO>> page(int page, int limit, Long menuId) throws Exception{
        return menuService.page(page, limit, menuId);
    }

    @PostMapping("/func/save")
    @ApiOperation(value = "新增菜单功能信息")
    @MenuAutho(desc = "新增菜单功能信息")
    public ResultCommon<SysBaseFuncDTO> save(@RequestBody @Validated(value = {Save.class}) SysBaseFuncDTO funcDTO,
                                                BindingResult bindingResult) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        return  menuService.save(funcDTO);
    }

    @PutMapping("/func/update")
    @ApiOperation(value = "更新菜单功能信息")
    @MenuAutho(desc = "更新菜单功能信息")
    public ResultCommon<SysBaseFuncDTO> update(@RequestBody @Validated(value = {Update.class}) SysBaseFuncDTO funcDTO,
                                             BindingResult bindingResult) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        return  menuService.update(funcDTO);
    }

    @DeleteMapping("/func/delete/{funcId}")
    @ApiOperation(value = "删除菜单功能信息")
    @MenuAutho(desc = "删除菜单功能信息")
    public ResultCommon<SysBaseFuncDTO> deleteFunc(@PathVariable Long funcId) throws Exception{
        return  menuService.deleteFunc(funcId);
    }

    @GetMapping("/search/by/{proMark}")
    @ApiOperation(value = "获取项目全部菜单信息")
    @MenuAutho(desc = "获取项目全部菜单信息")
    public ResultCommon<List<SysBaseMenuDTO>> searchMenus(@PathVariable String proMark) throws Exception{
        return menuService.searchMenus(proMark);
    }
}
